**************************************************************************************************************
**	Replication do-file for "Social Democracy and the Birth of Working-Class Representation in Europe"
**  Purpose: Produce Figures 1, 2, 3, and 5 for Norway 
*************************************************************************************************************

set more off

/* set the directory */

/**********************
**		Figure 1
***********************/
use "Norway.dta", clear
keep if year<1945

**		calculate the total number of MPs per year (winners)
egen total_mps_win =count(candidatename_ed) if elected==1 & (deputy!=0 | deputy!=.), by(year)

**		calculate the total number of MPs per year (losers)
egen total_mps_lose=count(candidatename_ed) if elected==0 & (deputy!=0 | deputy!=.), by(year)

**		calculate the total number of working-class MPs per year
foreach occ of varlist mp_manual {		 
				egen total_`occ'_win=total(`occ') if elected==1, by(year)					 
				gen `occ'_win_pct=(total_`occ'_win/total_mps_win)*100	
				egen total_`occ'_lose=total(`occ') if elected==0, by(year)					 
				gen `occ'_lose_pct=(total_`occ'_lose/total_mps_lose)*100	
}

**  	calculate the Labor's seat share
egen total_labor_win =count(candidatename_ed) if elected==1 & (deputy!=0 | deputy!=.) & socialist==1, by(year)
gen labor_share=(total_labor_win/total_mps_win)*100

duplicates drop mp_manual_win_pct mp_manual_lose_pct labor_share year, force

label var year "Year"
sort year
twoway (area labor_share year, lc(gs12) fc(gs12)) ///
		(connect mp_manual_win_pct year, lc(gs5) m(i) lw(.7))   ///
	    (connect mp_manual_lose_pct year, lc(gs5) lp(shortdash) m(i) lw(.7)),  ///
xlabel(1906 1909 1912 1915 1918 1921 1924 1927 1930 1933 1936 , labsize(4)) xsize(8)  ///
ytitle("Percent Workers", size(5))  ///
ylabel(0(10)50, labsize(4)) xtitle("Year", size(5)) title("Norway", size(5)) ///
name(candidates, replace)  legend (order(1 "Labor Seat Share" 2 "Worker (Won)" 3 "Worker (Lost)") row(1))  graphregion(color(white))
		
		
/******************************************
* Figure 2: Calculations for the Tree Maps
******************************************/
use "Norway.dta", clear

** pre-WWI (winners)
ta party_grp mp_manual if pr==0 & elected==1
di 25/36 /*Labor working-class MPs*/
di 7/36  /*Liberal working-class MPs*/
di 4/36  /*other working-class MPs*/

** Interwar (winners)
ta party_grp mp_manual if pr==1 & elected==1 & deputy==0
di 77/86 /*Labor working-class MPs*/
di 3/86   /*Communist working-class MPs*/
di 6/86  /*other working-class MPs*/

** pre-WWI (losers)
ta party_grp mp_manual if pr==0 & elected==0 
di 177/207 /*Labor working-class MPs*/
di 16/207  /*Liberal working-class MPs*/
di 14/207  /*other working-class MPs*/

** Interwar (losers)
ta party_grp mp_manual if pr==1 & elected==0 & deputy==0 
di 543/1415   /*Labor working-class MPs*/
di 564/1415 /*Communist working-class MPs*/
di 84/1415   /*Liberal working-class MPs*/
di 221/1415  /*other working-class MPs*/

/**********************
	Figure 3a
***********************/
use "Norway.dta", clear
replace manual_1910_smd=manual_1910_smd/100
replace manual_1910_smd=round(manual_1910_smd, .1) 
keep if year<1920
keep if pr==0 & socialist==1 
keep if round1==1

gen one=1
egen count_cand=count(one)               , by(manual_1910_smd)
egen count_wc=count(one)  if mp_manual==1, by(manual_1910_smd)
egen count_nwc=count(one) if mp_manual==0, by(manual_1910_smd)

collapse (mean) count_wc count_nwc count_cand, by(manual_1910_smd)
recode count_wc (.=0) 

gen wc =count_wc /count_cand 
gen nwc=count_nwc/count_cand 

replace manual_1910_smd=manual_1910_smd*100
replace wc=wc*100
replace nwc=nwc*100

gr bar wc nwc, over(manual_1910_smd) bar(1, fc(gs6)) bar(2, fc(gs13) lc(gs6)) stack ///
	graphregion(color(white)) ytitle("Class Background of Labor Candidates", size(5)) ///
	b1title("Percent Manual Workers in the Constituency", size(5)) legend(label(1 "Worker")) ///
	legend(label(2 "Non-Worker")) name(bar1, replace)

/**********************
	Figure 3b
***********************/
use "Norway.dta", clear
replace manual_1910_smd=manual_1910_smd/100
replace manual_1910_smd=round(manual_1910_smd, .1) 
keep if year<1920
keep if pr==0 & socialist==1 & elected==1
keep if round1==1

gen one=1
egen count_cand=count(one)               , by(manual_1910_smd)
egen count_wc=count(one)  if mp_manual==1, by(manual_1910_smd)
egen count_nwc=count(one) if mp_manual==0, by(manual_1910_smd)

collapse (mean) count_wc count_nwc count_cand, by(manual_1910_smd)
recode count_wc (.=0) 

gen wc =count_wc /count_cand 
gen nwc=count_nwc/count_cand

recode wc (.=0) 
replace manual_1910_smd=manual_1910_smd*100
replace wc=wc*100
replace nwc=nwc*100

** add a few rows to make the graphs easy to compare
set obs 8 
replace manual_1910_smd=10 in 6
replace manual_1910_smd=20 in 7
replace manual_1910_smd=30 in 8

gen wnwc=.
replace wnwc=100 in 6
replace wnwc=100 in 7
replace wnwc=100 in 8

gr bar wc nwc wnwc, over(manual_1910_smd) bar(1, fc(gs6)) bar(2, fc(gs13) lc(gs6)) bar(3, fc(white) lp(dash) lc(gs6)) stack ///
	graphregion(color(white)) ytitle("Class Background of Labor MPs", size(5)) ///
	b1title("Percent Manual Workers in the Constituency", size(5))  legend(label(1 "Worker")) legend(label(2 "Non-Worker")) legend(label(3 "Labor did not win seats")) legend(col(4)) name(bar2, replace)

** combine Figures 3a & 3b
gr combine bar1 bar2, graphregion(color(white)) xsize(10) name(g1, replace)

/**********************
	Figure 3c
***********************/
use "Norway.dta", clear
keep if pr==0 & socialist==1 
keep if year<1920
keep if round1==1

sum union_density if pr==0, detail
gen union_structure=.
replace union_structure=1 if union_density<.77057
replace union_structure=2 if union_density>=.97466 & union_density<=9.4951
replace union_structure=3 if union_density>9.4951
ta union_structure

gen one=1
egen count_cand=count(one)               , by(union_structure)
egen count_wc=count(one)  if mp_manual==1, by(union_structure)
egen count_nwc=count(one) if mp_manual==0, by(union_structure)

collapse (mean) count_wc count_nwc count_cand, by(union_structure)
recode count_wc (.=0) 

gen wc =count_wc /count_cand 
gen nwc=count_nwc/count_cand

recode wc (.=0) 
replace wc=wc*100
replace nwc=nwc*100

label define union_structure 1 "L" 2 "M" 3 "H", modify
label val union_structure union_structure

gr bar wc nwc, over(union_structure) bar(1, fc(gs6)) bar(2, fc(gs13) lc(gs6)) bar(3, fc(white) lp(dash) lc(gs6)) stack ///
	graphregion(color(white)) ytitle("Class Background of Labor Candidates", size(5)) ///
	b1title("Unionization in the Constituency", size(5))  legend(label(1 "Worker")) legend(label(2 "Non-Worker")) legend(label(3 "Labor did not win seats")) legend(col(3)) name(bar3, replace)


/**********************
	Figure 3d
***********************/
use "Norway.dta", clear
replace manual_1910_smd=manual_1910_smd/100
keep if pr==0 & socialist==1 & elected==1
keep if year<1920
keep if round1==1

gen union_structure=.
replace union_structure=1 if union_density<.77057
replace union_structure=2 if union_density>=.97466 & union_density<=9.4951
replace union_structure=3 if union_density>9.4951

gen one=1
egen count_cand=count(one)               , by(union_structure)
egen count_wc=count(one)  if mp_manual==1, by(union_structure)
egen count_nwc=count(one) if mp_manual==0, by(union_structure)

collapse (mean) count_wc count_nwc count_cand, by(union_structure)
recode count_wc (.=0) 

gen wc =count_wc /count_cand 
gen nwc=count_nwc/count_cand

recode wc (.=0) 
replace wc=wc*100
replace nwc=nwc*100

label define union_structure 1 "L" 2 "M" 3 "H", modify
label val union_structure union_structure

gr bar wc nwc, over(union_structure) bar(1, fc(gs6)) bar(2, fc(gs13) lc(gs6)) bar(3, fc(white) lp(dash) lc(gs6)) stack ///
	graphregion(color(white)) ytitle("Class Background of Labor MPs", size(5)) ///
	b1title("Unionization in the Constituency", size(5))  legend(label(1 "Worker")) legend(label(2 "Non-Worker")) legend(label(3 "Labor did not win seats")) legend(col(3)) name(bar4, replace)

** combine Figures 3c & 3d
gr combine bar3 bar4, graphregion(color(white)) xsize(10) name(g2, replace)

/**********************
	Figure 5
***********************/
use "Norway.dta", clear
keep if pr==1 & socialist==1 & deputy!=1 & rank<=magnitude
replace manual_1910_pr=manual_1910_pr/100
replace union_density=union_density/100

label define mp_manual 0 "Non-Worker" 1 "Worker", modify
label val mp_manual mp_manual

** A: Share of manual workers
ta manual_1910_pr
gen manual_structure=.
replace manual_structure=1 if manual_1910_pr<.4440789 
replace manual_structure=2 if manual_1910_pr>=.4440789 & manual_1910_pr<=.5146956
replace manual_structure=3 if manual_1910_pr>.5146956

label define manual_structure 1 "L" 2 "M" 3 "H", modify
label val manual_structure manual_structure

gr box seatrank  if party_grp=="Socialist",  over(mp_manual) over(manual_structure)   ///
		box(1, c(gs13)) marker(1, mc(gs13) ) box(2, c(gs6) ) marker(2, mc(gs6) mlabsize(o) ms(5)) asyvars   graphregion(color(white)) ///
		title("") yline(0, lp(dot) lc(gs2)) ///
	    b1title("Percent Manual Workers in the Constituency", size(7)) ytitle("Candidate's Seat Rank Security", size(7)) scheme(s2mono)  name(seatrank1, replace) 

** B: Unionization
ta union_density if pr==1
gen union_structure=.
replace union_structure=1 if union_density<.0142982 
replace union_structure=2 if union_density>=.0142982 & union_density<=.0630298
replace union_structure=3 if union_density>.0630298

label define union_structure 1 "L" 2 "M" 3 "H", modify
label val union_structure union_structure

gr box seatrank  if party_grp=="Socialist" ,  over(mp_manual) over(union_structure)   ///
		box(1, c(gs13)) marker(1, mc(gs13)) box(2, c(gs6)) marker(2, mc(gs6)) asyvars  graphregion(color(white)) ///
		title("") yline(0, lp(dot) lc(gs2)) ///
	    b1title("Unionization in the Constituency", size(7)) ytitle("Candidate's Seat Rank Security", size(7)) scheme(s2mono)  name(seatrank2, replace) 

** Combine
gr combine seatrank1 seatrank2, graphregion(color(white)) scheme(s2mono) col(3) xsize(10)